package com.ocom.netty.mj.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.resp.RespMachMessage;
import com.ocom.netty.mj.client.AiotClient;
import com.ocom.netty.mj.client.SrvpAccessDeviceClient;
import com.ocom.netty.mj.service.CompanyService;
import com.ocom.redis.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
@Resource
public class CompanyServiceImpl  implements CompanyService {
	@Autowired
	private SrvpAccessDeviceClient accessDeviceClient;

	@Autowired
	AiotClient aiotClient;

	@Resource
	RedisUtil redisUtil;

	@Override
	public Map<String,Object> getCompanyId(String machNo) {
		Map<String,Object> map=new HashMap<>();
		map.put("result",true);
		map.put("code","0000");
		map.put("companyId",0);

		Integer comId=(Integer) redisUtil.hmGet("tcpMach:"+machNo,"companyId");
		if(comId!=null && comId!=0){
			map.put("companyId",comId);
			return map;
		}

//		//调用服务  判断激活
//		RespMachMessage respMachMessage=accessDeviceClient.getDeviceState(machNo,"",1);
//		System.out.println(respMachMessage);
//		if(!respMachMessage.getCode().equals("0000")){
//			map.put("result",false);
//			map.put("code","0003");
//			return  map;
//		}
//		Map<String,Object> map1= JSON.parseObject(JSON.toJSONString(respMachMessage.getData()), Map.class);
//		Integer machStatus = (Integer) map1.get("machStatus");
//		if(machStatus==1){
//			//如果machStatus=1，填0x0701
//			map.put("result",false);
//			map.put("code","0701");
//			return  map;
//		}
		//机器号
		RespMachMessage respMachMessage1 = aiotClient.setMjDeviceCommTimeBySign(machNo, 1);
		if (!respMachMessage1.getCode().equals("0000")) {
			//如果machStatus=1，填0x0701
			map.put("result",false);
			map.put("code","0701");
			return  map;
		}
		JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(respMachMessage1.getData()));
		if(jsonObject.getInteger("status")==0){
			//如果machStatus=1，填0x0701
			map.put("result",false);
			map.put("code","0701");
			return  map;
		}
		Integer companyIdres = jsonObject.getInteger("comId");
		if(companyIdres==null || companyIdres == 0){
			//如果machStatus=1，填0x0701
			map.put("result",false);
			map.put("code","0701");
			return  map;
		}
		map.put("companyId",companyIdres);
		map.put("saasNo",jsonObject.getInteger("saasNo"));
		redisUtil.hmSet("tcpMach:"+machNo,"companyId",companyIdres);
		redisUtil.hmSet("tcpMach:"+machNo,"saasNo",jsonObject.getInteger("saasNo"));
		return  map;
	}
}

